package com.patni.frame.util;

import bean.FrameSetBean;

public class FrameSet {
private FrameSetBean setBean;

int[] widths;
int[] heights;

public FrameSet(FrameSetBean setBean){
	this.setBean = setBean;
}

public FrameSetBean getSetBean() {
	return setBean;
}

public void setSetBean(FrameSetBean setBean) {
	this.setBean = setBean;
}


public void init(){
	int rowNum = setBean.getTable().getRow();
	int columnNum = setBean.getTable().getColumn();

	int flag = 0;
	int temp = 0;
	
	widths=new int[columnNum];
	
	for(int i=0;i<columnNum;i++){
		String item = setBean.getTable().getColumnWidth()[i];
		if(!item.substring(item.length()-1).equals("%")){
		if(item.equals("0")){
			widths[i] = 0;
			flag = i;		
		}else{
			widths[i] = Integer.parseInt(item);
		}
		}else{
			item = item.substring(0,item.length()-1);
			widths[i] =  Integer.parseInt(item)*setBean.getMyBounds().getWidth()/100;
		}
	}
	for(int item : widths){
		temp+=item;
	}
	widths[flag] = setBean.getMyBounds().getWidth() - temp;
	
	flag = 0;
	temp = 0;
	
	heights=new int[rowNum];
	for(int i=0;i<rowNum;i++){
		String item = setBean.getTable().getRowHeight()[i];
		if(!item.substring(item.length()-1).equals("%")){
		if(item.equals("0")){
			heights[i] = 0;
			flag = i;	
		}else{
			heights[i] = Integer.parseInt(item);
		}
		}else{
			item = item.substring(0,item.length()-1);
			heights[i] =  Integer.parseInt(item)*setBean.getMyBounds().getHeight()/100;
		}
	}
	for(int item : heights){
		temp+=item;
	}
	heights[flag] = setBean.getMyBounds().getHeight() - temp;
	
	int windowsSize = setBean.getComponent().size();
	int whereSize = setBean.getTable().getWhere().size();
	int size = (windowsSize > whereSize ? whereSize : windowsSize);
	for(int i=0;i<size;i++){
		int[] whereItem=setBean.getTable().getWhere().get(i);
		int[] bounds=convert(whereItem);
		setBean.getComponent().get(i).setBounds(bounds[0], bounds[1], bounds[2], bounds[3]);
	}
}



public int[] convert(int[] whereItem){
	int[] bounds=new int[4];
	
	bounds[0]=setBean.getMyBounds().getX();
	for(int i=0;i<whereItem[0];i++){
	bounds[0]+=widths[i];
	}
	
	bounds[1]=setBean.getMyBounds().getY();
	for(int j=0;j<whereItem[1];j++){
		bounds[1]+=heights[j];
	}
	
	bounds[2]=0;
	for(int i=whereItem[0];i<whereItem[0]+whereItem[2];i++){
		bounds[2]+=widths[i];
	}
	
	bounds[3]=0;
	for(int j=whereItem[1];j<whereItem[1]+whereItem[3];j++){
		bounds[3]+=heights[j];
	}
	return bounds;
}

public int[] getWidths() {
	return widths;
}

public void setWidths(int[] widths) {
	this.widths = widths;
}

public int[] getHeights() {
	return heights;
}

public void setHeights(int[] heights) {
	this.heights = heights;
}
}
